#include "statusnotifiermodel.h"

StatusNotifierModel::StatusNotifierModel()
{

}

void StatusNotifierModel::addSource(const QString &serviceName, StatusNotifierItemSource *itemSource)
{
    Q_UNUSED(serviceName);
    //绑定itemSource的属性更新信号，收到信号后更新数据
    connect(itemSource, &StatusNotifierItemSource::dataUpdated, this, &StatusNotifierModel::dataUpdated);
}

void StatusNotifierModel::removeSource(const QString &serviceName)
{
    if (m_itemServices.contains(serviceName)) {
        int index= m_itemServices.indexOf(serviceName);
        this->removeRow(index);
        m_itemServicesInfo.removeAt(index);
        m_itemServices.removeAll(serviceName);
    }
}

void StatusNotifierModel::removeAllSources()
{
    this->clear();
    m_itemServices.clear();
}

QList<QMap<QString, StatusNotifierItemSource*>> StatusNotifierModel::getItemServicesInfo()
{
    return m_itemServicesInfo;
}

void StatusNotifierModel::dataUpdated(const QString &serviceName, StatusNotifierItemSource *itemSource, const QMap<uint, QVariant> &data)
{
    qDebug()<<"----- serviceName:"<<serviceName;
    qDebug()<<"----- data:"<<data;
    QStandardItem *dataItem;
    if (m_itemServices.contains(serviceName)) {
        dataItem = this->item(m_itemServices.indexOf(serviceName));
    } else {
        dataItem = new QStandardItem();
//        dataItem->setData(QStringLiteral("StatusNotifier"), static_cast<int>(BaseRole::ItemType));
//        dataItem->setData(true, static_cast<int>(BaseRole::CanRender));
    }

    dataItem->setData(data.value(Role::Title), Qt::DisplayRole);
    QVariant icon = data.value(Role::Icon);
    if (icon.isValid() && icon.canConvert<QIcon>() && !icon.value<QIcon>().isNull()) {
        dataItem->setData(icon, Qt::DecorationRole);
    } else {
        dataItem->setData(data.value(Role::IconName), Qt::DecorationRole);
    }
//    dataItem->setData(data.value("Id"), static_cast<int>(BaseRole::ItemId));
//    dataItem->setData(data.value("Category"), static_cast<int>(BaseRole::Category));
//    dataItem->setData(serviceName, static_cast<int>(Role::DataEngineSource));
    updateItemData(dataItem, data, Role::Id);
    updateItemData(dataItem, data, Role::WindowId);
    updateItemData(dataItem, data, Role::Category);
    updateItemData(dataItem, data, Role::Status);
    updateItemData(dataItem, data, Role::StatusChanged);
    updateItemData(dataItem, data, Role::Title);
    updateItemData(dataItem, data, Role::TitleChanged);
    updateItemData(dataItem, data, Role::ToolTipIcon);
    updateItemData(dataItem, data, Role::ToolTipSubTitle);
    updateItemData(dataItem, data, Role::ToolTipTitle);
    updateItemData(dataItem, data, Role::ToolTipChanged);
    updateItemData(dataItem, data, Role::Icon);
    updateItemData(dataItem, data, Role::IconName);
    updateItemData(dataItem, data, Role::IconThemePath);
    updateItemData(dataItem, data, Role::IconsChanged);
    updateItemData(dataItem, data, Role::OverlayIconName);
    updateItemData(dataItem, data, Role::AttentionIcon);
    updateItemData(dataItem, data, Role::AttentionIconName);
    updateItemData(dataItem, data, Role::AttentionMovieName);
    updateItemData(dataItem, data, Role::ItemIsMenu);

    //新Item数据要加入模型和m_itemServices列表中
    if (!m_itemServices.contains(serviceName)) {
        m_itemServices.append(serviceName);
        QMap<QString, StatusNotifierItemSource*> itemMap;
        itemMap.insert(serviceName, itemSource);
        m_itemServicesInfo.append(QMap<QString, StatusNotifierItemSource*>(itemMap));
        appendRow(dataItem);
    }
}

void StatusNotifierModel::updateItemData(QStandardItem *dataItem,
                                         const QMap<uint, QVariant> &data, const Role role)
{
    int roleId = static_cast<int>(role);
    if(role == Role::Id) {
        qDebug()<<">>>> StatusNotifierModel::role: "<<role;
        qDebug()<<">>>> StatusNotifierModel::roleId: "<<roleId;
    }
    dataItem->setData(data.value(roleId), roleId);
//    dataItem->setData(data.value("Id"), roleId);
}
